﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

using InterfacesTransformation;

namespace MyWordSegmentorLM
{
    public partial class LongestMatchingWordSegmentorPlugin : UserControl, IWordSegmentor
    {
        public LongestMatchingWordSegmentorPlugin()
        {
            InitializeComponent();
        }

        private IHost pluginHost = null;
        public IHost PluginHost
        {
            get
            {
                return pluginHost;
            }
            set
            {
                this.pluginHost = value;
            }
        }

        private string pluginName = "Vietnamese Word Segmentor Using Longest Matching";
        public string PluginName
        {
            get
            {
                return this.pluginName;
            }
        }

        private string pluginDescription = "Segment vietnamese sentences or phrases into words.";

        public string PluginDescription
        {
            get { return pluginDescription; }
            set { pluginDescription = value; }
        }

        public UserControl PluginUserControl
        {
            get
            {
                return this;
            }
        }

        public void Initialize()
        {
        }

        public void PluginDispose()
        {

        }

        private string dicPath = string.Empty;

        public string DicPath
        {
            get { return dicPath; }
            set { dicPath = value; }
        }

        #region Interfaces Implementation
        public string Segment(string inputStr)
        {            
            return SegmentString(inputStr, dicPath);
        }

        public List<string> Segment(List<string> inputStrList)
        {          
            List<string> coreString = new List<string>();
            FileStream fs = new FileStream(dicPath, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs);

            while (!sr.EndOfStream)
            {
                coreString.Add(sr.ReadLine());
            }
            
            List<string> result = new List<string>();

            foreach (string curStr in inputStrList)
            {
                string parsedStr = string.Empty;
                parsedStr = Utilities.StringLib.ProcessString.StripDiacritics(curStr).ToUpper();

                string[] splitWords = Utilities.StringLib.ProcessString.SplitTermsAccordingToCoreStrings(coreString.ToArray(), curStr);

                foreach (string curWord in splitWords)
                {
                    parsedStr += " " + curWord.Replace(' ', '_');
                }
                result.Add(parsedStr);
            }
            sr.Close();
            fs.Close();
            return result;
        }
        #endregion

        #region Fix Functions
        private string SegmentString(string inputStr, string dictionaryFilePath)
        {
            string parsedStr = string.Empty;
            parsedStr = Utilities.StringLib.ProcessString.StripDiacritics(inputStr).ToUpper();

            List<string> coreString = new List<string>();
            FileStream fs = new FileStream(dictionaryFilePath, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs);

            while (!sr.EndOfStream)
            {
                coreString.Add(sr.ReadLine());
            }

            string[] splitWords = Utilities.StringLib.ProcessString.SplitTermsAccordingToCoreStrings(coreString.ToArray(), inputStr);

            foreach (string curWord in splitWords)
            {
                parsedStr += " " + curWord.Replace(' ', '_');
            }
            sr.Close();
            fs.Close();
            return parsedStr;
        }
        #endregion

        #region GUI Event Handlers
        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDlg = new OpenFileDialog();
            openFileDlg.InitialDirectory = System.IO.Directory.GetCurrentDirectory();
            openFileDlg.Multiselect = false;
            openFileDlg.FileName = string.Empty;

            if (openFileDlg.ShowDialog() == DialogResult.OK)
            {
                textBoxDictionaryPath.Text = openFileDlg.FileName;
                this.dicPath = openFileDlg.FileName;
            }
        }
        #endregion
    }
}
